package com.imooc.pan.lock.redis;

import com.imooc.pan.lock.core.LockConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.integration.support.locks.LockRegistry;

/**
 * 基于 Redis 的分布式锁
 * 该方案集成 spring-data-redis
 */

@SpringBootConfiguration
@Slf4j
public class RedisLockConfig {
    @Bean
    public LockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory) {
        RedisLockRegistry redisLockRegistry =
                new RedisLockRegistry(redisConnectionFactory, LockConstants.R_PAN_LOCK);
        log.info("redis lock is loaded successfully");
        return redisLockRegistry;
    }
}
